package com.xoms.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.controller.springmvc.req.*;
import com.xoms.admin.model.*;
import com.xoms.admin.service.RoomService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: tzz(Tian)
 * Date: 2018/4/28 13:04
 */
@Component
public class RoomController {
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    RoomService roomServiceImpl;

    public ListResult<RRoomDimensionBean> getDimensions(HttpServletRequest rquest, HttpServletResponse response) {
        ListResult<RRoomDimensionBean> result = new ListResult<RRoomDimensionBean>();
        try {
            PageResult<RRoomDimensionBean> listData = roomServiceImpl.getRoomDimensions();
            if (listData != null) {
                result.setData(listData.getData());
                result.setTotalSize(listData.getCount());
            }

        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomDimensionDetailBean> getDimensionDetails(GetRoomDimensionDetailsParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomDimensionDetailBean> result = new ListResult<RRoomDimensionDetailBean>();
        try {
            result = roomServiceImpl.getDimensionDetails(param.getDimensionId());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomDimensionDetailBean> getDimensionAllDetail(HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomDimensionDetailBean> result = new ListResult<RRoomDimensionDetailBean>();
        try {
            result = roomServiceImpl.getDimensionAllDetail();
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomDimensionScaleBean> getDimensionScaleDownList(GetRoomDimensionScaleParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomDimensionScaleBean> result = new ListResult<RRoomDimensionScaleBean>();
        try {
            String[] pids = param.getPids().split(",");
            result = roomServiceImpl.getDimensionScaleDownList(param.getDimensionId(), param.getDid(), pids);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomDimensionScaleListBean> getDimensionScaleList(HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomDimensionScaleListBean> result = new ListResult<RRoomDimensionScaleListBean>();
        try {
            result = roomServiceImpl.getDimensionScaleList();
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RRoomDimensionDetailBean> editDimensionDetail(EditRoomDimensionDetailParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RRoomDimensionDetailBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RoomDimensionDetailBean data = new RoomDimensionDetailBean();
            data.setName(param.getName().trim());
            data.setRemark(param.getRemark());
            data.setStatus(NumUtil.toInt(param.getStatus(), 1));
            data.setDid(param.getId());
            data.setDimensionId(param.getDimensionId());

            //成功后返回编辑后对象
            RoomDimensionDetailBean res = roomServiceImpl.editDimensionDetail(account, data);
            RRoomDimensionDetailBean bean = new RRoomDimensionDetailBean();
            BeanUtils.copyProperties(res, bean);
            result.setData(bean);
            result.setFlag(bean != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult delDimensionDetail(DelByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

            boolean flag = roomServiceImpl.delDimensionDetail(account, param.getId());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RRoomDimensionScaleBean> editDimensionScale(EditRoomDimensionScaleParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RRoomDimensionScaleBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RoomDimensionScaleBean data = new RoomDimensionScaleBean();
            Object json = JSON.parse(param.getScales());
            // 这里可以验证对应关系是否正确
            //
            Map<String, String> map = (Map<String, String>)json;
//            map.values()
//            data.setDimeRalation("['"+String.join("','", map.values())+"']");
//            data.setDimeRalation();
            data.setDimeRalation("["+param.getScales().replaceAll("[{|}]","").replaceAll("\"[\\w|\\d]*\":","").replaceAll("\"","'")+"]");
            data.setRemark(param.getRemark());
            data.setStatus(NumUtil.toInt(param.getStatus(), 1));
            data.setSid(param.getId());
            data.setScale(param.getScale());

            //成功后返回编辑后对象
            RoomDimensionScaleBean res = roomServiceImpl.editDimensionScale(account, data);
            RRoomDimensionScaleBean resData = new RRoomDimensionScaleBean();
            BeanUtils.copyProperties(res, resData);
            result.setData(resData);
            result.setFlag(res != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult delDimensionScale(DelByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

            boolean flag = roomServiceImpl.delDimensionScale(account, param.getId());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomBean> getRoomList(GetRoomListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomBean> result = new ListResult<RRoomBean>();
        try {
            result = roomServiceImpl.getRoomList(param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RRoomBean> editRoom(EditRoomParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RRoomBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RoomBean data = new RoomBean();
//            Object json = JSON.parse(param.getScales());
            // 这里可以验证对应关系是否正确
//            Map<String, String> map = (Map<String, String>)json;
//            map.values()
//            data.setDimeRalation("['"+String.join("','", map.values())+"']");
//            data.setDimeRalation();
            String scale = param.getScale();
//            String scales = param.getScales().replaceAll("[{|}]","").replaceAll("\"[\\w|\\d]*\":","").replaceAll("\"","");
            data.setRid(param.getId());
            data.setMaxCapacity(param.getMaxCapacity());
            data.setNumber(param.getNumber());
            data.setName(param.getName());
            // 影院id，从账号信息中获取
            // TODO 账号信息中需要添加影院归属id
            data.setTheatreId("123123123");

            //成功后返回编辑后对象
            RoomBean res = roomServiceImpl.editRoom(account, data, scale);
            RRoomBean resData = new RRoomBean();
            BeanUtils.copyProperties(res, resData);
            result.setData(resData);
            result.setFlag(res != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult delRoom(DelByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

            boolean flag = roomServiceImpl.delRoom(account, param.getId());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }


    public ListResult<RTimeBucketBean> getTimeBucketList(HttpServletRequest request, HttpServletResponse response) {
        ListResult<RTimeBucketBean> result = new ListResult<RTimeBucketBean>();
        try {
            result = roomServiceImpl.getTimeBucketList();
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RTimeBucketBean> addTimeBucket(AddTimeBucketParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RTimeBucketBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            TimeBucketBean data = new TimeBucketBean();
            data.setName(param.getName());
            data.setStatus(0);
            // 影院id，从账号信息中获取
            // TODO 账号信息中需要添加影院归属id
            data.setTheatreId("123123123");

            //成功后返回编辑后对象
            RTimeBucketBean resData = roomServiceImpl.addTimeBucket(account, data, param.getBuckets());
            result.setData(resData);
            result.setFlag(resData != null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult delTimeBucket(GetByNameParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

            boolean flag = roomServiceImpl.delTimeBucket(account, param.getName());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }


    public ListResult<RTimeBucketBean> getTimeBucket(GetByNameParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RTimeBucketBean> result = new ListResult<RTimeBucketBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getTimeBucket(account, param.getName());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult addDimePrice(AddDimePriceParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RoomDimePriceBean data = new RoomDimePriceBean();
            data.setCreatorId(account.getAccid());
            data.setStartTime(new Timestamp(param.getDateB()));
            data.setEndTime(new Timestamp(param.getDateE()));
            data.setPriceType(NumUtil.toInt(param.getPriceType(),0));
            data.setStatus(0);

            boolean flag = roomServiceImpl.addDimePrice(account, data, param.getBucketName(), param.getScales().replaceAll("\"","'"), param.getBucketPrices());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public BaseResult addRoomPrice(AddRoomPriceParam param, HttpServletRequest request, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RoomPriceBean data = new RoomPriceBean();
            data.setCreatorId(account.getAccid());
            data.setStartTime(new Timestamp(param.getDateB()));
            data.setEndTime(new Timestamp(param.getDateE()));
            data.setPriceType(NumUtil.toInt(param.getPriceType(),0));
            data.setRoomId(param.getId());
            data.setStatus(0);

            boolean flag = roomServiceImpl.addRoomPrice(account, data, param.getBucketName(), param.getBucketPrices());
            result.setFlag(flag);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomPriceBean> getRoomPriceList(GetRoomPriceListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomPriceBean> result = new ListResult<RRoomPriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getRoomPriceList(account, param.getId(), param.getPage(), param.getPageSize());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomPriceBean> getRoomPriceDetail(GetRoomPriceDetailParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomPriceBean> result = new ListResult<RRoomPriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getRoomPriceDetail(account, param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomPriceBean> getRoomCurrPrice(GetByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomPriceBean> result = new ListResult<RRoomPriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getRoomCurrPrice(account, param.getId());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RRoomMonthPriceBean> getRoomMonthPrice(GetRoomMonthPriceParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RRoomMonthPriceBean> result = new ListResult<RRoomMonthPriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getRoomMonthPrice(account, param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RDimePriceBean> getDimePriceList(GetDimePriceListParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RDimePriceBean> result = new ListResult<RDimePriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getDimePriceList(account, param.getDateB(), param.getDateE(), param.getPriceType(), param.getScale(), param.getPage(), param.getPageSize());
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ListResult<RDimePriceBean> getDimePriceDetail(GetDimePriceDetailParam param, HttpServletRequest request, HttpServletResponse response) {
        ListResult<RDimePriceBean> result = new ListResult<RDimePriceBean>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            result = roomServiceImpl.getDimePriceDetail(account, param);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

    public ObjectResult<RRoomBean> getRoomById(DelByIdParam param, HttpServletRequest request, HttpServletResponse response) {
        ObjectResult<RRoomBean> result = new ObjectResult<>();
        try {
            AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
            RRoomBean bean = roomServiceImpl.getRoomById(param.getId());
            result.setData(bean);
            result.setFlag(bean!=null);
        } catch (ServiceException e) {
            result.setServiceException(e);
        }
        return result;
    }

}
